﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows.Media.Imaging;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Net.Sockets;


namespace Master
{
    /**
     *  CarDetails is a serializable class used for sending data over a network.
     *  @Creator: J.J. Burgers
     *  @Changes made by:
     *      - T.T. Dusseldorp
     */
    //
    [Serializable]
    public class CarDetails
    {
        /*************
         * Variables *
         *************/
         // Used for de serializing a CarDetails object.
        [NonSerialized]
        public List<byte> TransmissionBuffer = new List<byte>();
        // buffer used to serialize and deserialize CarDetails object 
        [NonSerialized]
        public byte[] buffer = new byte[4096];

        private Type carType;
        private double speed;

        /**
         * An constructer wich does nothing more then making an object of CarDetails with empty variables.
         * It is used to make an empty Cardetails object.
         */
        public CarDetails()
        {
            
        }
        /**
         * Second constructer that gets the speed and cartype out of car and puts them the new created object.         
         */
        public CarDetails(Car car) 
        {
            carType = car.GetType();
            speed = car.Speed;
        }

        /********************
         * PUBLIC FUNCTIONS *
         ********************/
        /**
         * Transforms CarDetails object to a byte array and returns it.
         */
        public byte[] Serialize()
        {
            BinaryFormatter bin = new BinaryFormatter();
            MemoryStream mem = new MemoryStream();
            bin.Serialize(mem, this);
            return mem.GetBuffer();
        }

        /**
         * Transforms the serialized array in TransmissionBuffer back to an CarDetails object.         
         */
        public CarDetails DeSerialize()
        {
            byte[] dataBuffer = TransmissionBuffer.ToArray();
            BinaryFormatter bin = new BinaryFormatter();
            MemoryStream mem = new MemoryStream();
            mem.Write(dataBuffer, 0, dataBuffer.Length);
            mem.Seek(0, 0);
            return (CarDetails)bin.Deserialize(mem);
        }
        // A get request for the carType variable.
        public Type getCarType() { return this.carType; }
        // A get request for the speed variable.
        public double getSpeed() { return this.speed; }
        
    }
}
